package com.cuit.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cuit.pojo.Doctor;
import com.cuit.pojo.DoctorOrder;
import com.cuit.service.D_OrderService;
import com.cuit.service.DoctorService;

/**
 * Servlet implementation class d_register
 */
@WebServlet("/registerr")
public class d_register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public d_register() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int id=Integer.parseInt(request.getParameter("username"));
		String password=request.getParameter("password");
		Doctor d=new Doctor(id,password);
		System.out.println(d.getId()+" "+d.getPassword());

		//获取预约表
		String times[][]=new String[7][3];
		int maxs[]=new int[7];
		DoctorOrder or=new DoctorOrder(id);
		D_OrderService os=new D_OrderService(or);
		times=os.getTimes(id);
		maxs=os.getmaxs(id);
		request.getSession().setAttribute("maxs",maxs);
		request.getSession().setAttribute("times",times);

		DoctorService user=new DoctorService(d);
		request.getSession().setAttribute("doc",d);
		if(user.enroll()==true) {
			//response.sendRedirect("/hospitalSystem_war_exploded/doctor_cm/personal_data/d_personal.jsp");
			if(d.getName()!=null&&d.getAge()!=0&&d.getSex()!=null&&d.getGrade()!=null&&
					d.getDepartment()!=null&&d.getProfile()!=null){
				response.sendRedirect("/hospital/doctor_cm/personal_data/d_per.jsp");
			}else{
				response.sendRedirect("/hospital/doctor_cm/personal_data/d_personal.jsp");
			}

		}else {
			response.getWriter().write(d.getIssue());
			String contextPath = request.getContextPath();
			response.sendRedirect("/hospital/doctor_cm/login/doctor_register.jsp");
			//response.setHeader("refresh","1;url="+contextPath+"/doctor_cm/login/doctor_register.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
